//
//  MQApplyagentViewController.m
//  meiquan
//
//  Created by hejingjin on 2017/10/28.
//  Copyright © 2017年 mq. All rights reserved.
//

#import "MQApplyagentViewController.h"
#import "MQapplyagentDetailview.h"
#import "MQBaseUserInfoModel.h"
#import "NSString+Extension.h"

#import "MQadviews.h"
#import "MQAdmodels.h"

@interface MQApplyagentViewController ()

@property(nonatomic,strong)MQapplyagentDetailview *applyagentview;

@property(nonatomic,strong)UIScrollView *bgview;

@property(nonatomic,strong)MQadviews *adviews;

@property(nonatomic,strong)MQAdmodels *admodels;

@end

@implementation MQApplyagentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"代理商申请";
    
    [self setUpMainInterface];
    
    [self getaddata];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)getaddata{
    
    NSArray *array = @[@"page1",@"@page2",@"page3"];
    int x = arc4random() % 3;
    
    MQBaseUserInfoModel *userinfo = [MQBaseUserInfoModel shareInstant];
    [userinfo loadUserinfoFromSadeBox];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:userinfo.userId forKey:@"userId"];
    [param setValue:array[x] forKey:@"code"];
    [param setValue:@"page1" forKey:@"code"];
    
    [MQRequestTools POSTWithMethod:adv_index params:param success:^(id json) {
        
        NSDictionary *dic = [json valueForKey:@"data"];
        _admodels = [MQAdmodels mj_objectWithKeyValues:dic];
        if (_admodels.advId) {
            [_adviews.adimageviews sd_setImageWithURL:[NSURL URLWithString:_admodels.pic] placeholderImage:[UIImage imageNamed:@""]];
        }else{
            _adviews.bst_height = 0;
            _bgview.bst_y = NAVHEIGHT;
            
        }
        
        
        UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [_adviews addGestureRecognizer:tapGesturRecognizer];
        
    } failure:^(NSError *error) {
        
    }];
    
    
}

-(void)tapAction:(UITapGestureRecognizer *)obj{
    NSLog(@"%@",_admodels.url);
    MQAdDetailViewController *advc = [[MQAdDetailViewController alloc] init];
    advc.url = _admodels.url;
     advc.type = _admodels.type;
    [self.navigationController pushViewController:advc animated:YES];
}


-(void)setUpMainInterface{
    
    self.view.backgroundColor = BSTColorFromRGB(0xefefef);
    
    
    _adviews = [MQadviews loadadviews];
    _adviews.bst_x = 0;
    _adviews.bst_width = BSTScreenW;
    _adviews.bst_height = 70 *Persion;
    _adviews.bst_y = NAVHEIGHT;
    [self.view addSubview:_adviews];
    
    
    MQapplyagentDetailview *applyagentview = [MQapplyagentDetailview mqapplyagentDetailview];
    
    applyagentview.bst_x = 0;
    applyagentview.bst_y = 0;
    applyagentview.bst_width = BSTScreenW;
    applyagentview.bst_height = 844;
    _applyagentview = applyagentview;
    
    _applyagentview.submitbutton.layer.cornerRadius=3.f;
    _applyagentview.submitbutton.layer.masksToBounds = YES;
    [_applyagentview.submitbutton addTarget:self action:@selector(submitbuttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    UIScrollView *bgviwe = [[UIScrollView alloc] initWithFrame:CGRectMake(0, NAVHEIGHT + 70 * Persion, BSTScreenW, BSTScreenH)];
    
    
    [bgviwe addSubview:_applyagentview];
    _bgview = bgviwe;
    [bgviwe setContentSize:CGSizeMake(BSTScreenW, 844 + 70 * Persion)];
    
    [self.view addSubview:bgviwe];
    
}

-(void)submitbuttonPressed:(UIButton *)sender{
    
    NSString *shangji = _applyagentview.shangjiweixin.text;
    NSString *wodeweixin = _applyagentview.wodeweixin.text;
    NSString *tuijianren = _applyagentview.tuijianren.text;
    NSString *canpingma = _applyagentview.canpingma.text;
    NSString *dailidengji = _applyagentview.dailidengji.text;
    NSString *zhenshixinming = _applyagentview.zhenshiname.text;
    NSString *shenfenzheng = _applyagentview.idcard.text;
    NSString *youxiang = _applyagentview.eamil.text;
    
    NSString *shopone = _applyagentview.shoponename.text;
    NSString *count1 = _applyagentview.countone.text;
    NSString *shop2 = _applyagentview.shoptwoField.text;
    NSString *count2 = _applyagentview.counttwo.text;
    NSString *shop3 = _applyagentview.shopthree.text;
    NSString *count3 = _applyagentview.countthree.text;
    
    if ([NSString isBlankString:shangji]) {
        [iToastUtil displayMsg:@"请输入上级微信号"];
        return;
    }
    if ([NSString isBlankString:wodeweixin]) {
        [iToastUtil displayMsg:@"请输入我的微信号"];
        return;
    }
    if ([NSString isBlankString:tuijianren]) {
        [iToastUtil displayMsg:@"请输入推荐人"];
        return;
    }
    if ([NSString isBlankString:canpingma]) {
        [iToastUtil displayMsg:@"请输入产品码"];
        return;
    }
    if ([NSString isBlankString:dailidengji]) {
        [iToastUtil displayMsg:@"请输入代理等级"];
        return;
    }
    if ([NSString isBlankString:zhenshixinming]) {
        [iToastUtil displayMsg:@"请输入真实姓名"];
        return;
    }
    if ([NSString isBlankString:shenfenzheng]) {
        [iToastUtil displayMsg:@"请输入身份证"];
        return;
    }
    if ([NSString isBlankString:youxiang]) {
        [iToastUtil displayMsg:@"请输入邮箱"];
        return;
    }
    if ([NSString isBlankString:shopone]) {
        [iToastUtil displayMsg:@"请输入店铺名称一"];
        return;
    }
    if ([NSString isBlankString:count1]) {
        [iToastUtil displayMsg:@"请输入店铺一账号"];
        return;
    }
    if ([NSString isBlankString:shop2]) {
        [iToastUtil displayMsg:@"请输入店铺名称二"];
        return;
    }
    if ([NSString isBlankString:count2]) {
        [iToastUtil displayMsg:@"请输入店铺二账号"];
        return;
    }
    if ([NSString isBlankString:shop3]) {
        [iToastUtil displayMsg:@"请输入店铺名称三"];
        return;
    }
    if ([NSString isBlankString:count3]) {
        [iToastUtil displayMsg:@"请输入店铺三账号"];
        return;
    }


    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    MQBaseUserInfoModel *userinfo = [MQBaseUserInfoModel shareInstant];
    [userinfo loadUserinfoFromSadeBox];
    [param setValue:userinfo.userId forKey:@"userId"];
     [param setValue:shangji forKey:@"parent"];
     [param setValue:wodeweixin forKey:@"weixin"];
     [param setValue:tuijianren forKey:@"referrer"];
     [param setValue:canpingma forKey:@"code"];
     [param setValue:dailidengji forKey:@"level"];
//     [param setValue:@"铜牌代理" forKey:@"level"];
     [param setValue:zhenshixinming forKey:@"realName"];
     [param setValue:shenfenzheng forKey:@"cardId"];
     [param setValue:youxiang forKey:@"email"];
     [param setValue:shopone forKey:@"shop1"];
     [param setValue:count1 forKey:@"account1"];
     [param setValue:shop2 forKey:@"shop2"];
     [param setValue:count2 forKey:@"account2"];
     [param setValue:shop3 forKey:@"shop3"];
     [param setValue:count3 forKey:@"account3"];
    [MQRequestTools POSTWithMethod:agent_apply params:param success:^(id json) {
        
        [self.navigationController popViewControllerAnimated:YES];
    
    } failure:^(NSError *error) {
        
        if ([error isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dic = (NSDictionary *)error;
            [iToastUtil displayMsg:[dic valueForKey:@"msg"]];
        }
        

    }];
    
    
    
    
}
@end
